package com.example.demo.msg.socketio.handler;/**
 * Created by kete on 2017-12-19.
 */

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.example.demo.msg.socketio.vo.MessageInfoVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
 * 作者：kete
 * 创建时间：2017-12-19 14:48
 * 功能描述：
 * 版本：
 */
@Component
@ConditionalOnProperty(prefix = "guns", name = "web-socket-open", havingValue = "true")
public class MessageEventHandler {

    private static Logger logger = LoggerFactory.getLogger(MessageEventHandler.class);

    @Autowired
    private SocketIOServer socketIOServer;



    @OnConnect
    public void onConnect(SocketIOClient client){
        logger.info("connect success");

    }

    @OnDisconnect
    public void  onDisconnect(SocketIOClient client){
        logger.info("disconnect success");
    }

    @OnEvent(value = "joinRoom")
    public void joinRoomEvent(SocketIOClient client, AckRequest request, MessageInfoVo messageInfoVo){
        if(StringUtils.isNotEmpty(messageInfoVo.getRoomId()))
        client.joinRoom(messageInfoVo.getRoomId());
    }

    @OnEvent(value = "messageEvent")
    public void messageEvent(SocketIOClient client, AckRequest request, MessageInfoVo messageInfoVo){

       socketIOServer.getRoomOperations(messageInfoVo.getRoomId()).sendEvent("messageEvent",messageInfoVo);

    }
}
